package nero.yncc.com.createart.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterMyOrder;
import nero.yncc.com.createart.base.BaseFragment;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanOrder;
import nero.yncc.com.createart.utils.HttpUtils;

import static android.view.View.GONE;

/**
 * Created by nero on 2017/7/12.
 */

public class FragmentMyOrder extends BaseFragment {
    public static  final  int STATE_ALL=0,STATE_WAITEPAY=1,STATE_WAITFA=2,STATE_WAITSHOU=3,STATE_WAITCOMMENT=4;

    ArrayList<BeanOrder> arrayList;
    AdapterMyOrder adapterMyOrder;
    PullToRefreshListView pullToRefreshListView;
    LinearLayout ll_nodata;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.single_pulltorefresh_notitle,null);
        int state_int=getArguments().getInt("state",0);
//        空为全部订单, WAITPAY:待付款WAITSEND:待发货WAITRECEIVE:待收货WAITCCOMMENT:已完成
        switch (state_int){
            case STATE_ALL:
                state="";
                break;
            case STATE_WAITEPAY:
                state="WAITPAY";
                break;
            case STATE_WAITFA:
                state="WAITSEND";
                break;
            case STATE_WAITSHOU:
                state="WAITRECEIVE";
                break;
            case STATE_WAITCOMMENT:
                state="WAITCCOMMENT";
                break;
        }
        arrayList=new ArrayList<>();
        adapterMyOrder=new AdapterMyOrder(getContext(),arrayList);
        pullToRefreshListView=view.findViewById(R.id.pl_list);
        pullToRefreshListView.setAdapter(adapterMyOrder);
        ll_nodata=view.findViewById(R.id.ll_nodata);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        initAll();
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=1;
                arrayList.clear();
                adapterMyOrder.notifyDataSetChanged();
                canLoad=true;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getData();
            }
        });

        if(state_int==STATE_ALL && arrayList.size()==0){
            pullToRefreshListView.setRefreshing();
        }
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            if(arrayList!=null && arrayList.size()==0){
                pullToRefreshListView.setRefreshing();
            }
        }
    }

    String state="";
    int pageSize=10;
    int page=1;
    boolean canLoad=true;
    @Override
    protected void getData() {
        super.getData();
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("user_id",userBean.getUser_id());
        hashMap.put("sate",state);
        hashMap.put("pagesize",pageSize+"");
        hashMap.put("pageindex",page+"");

        new HttpUtils().post(getActivity(), Constants.API_GETORDER_LIST, hashMap, null, HttpUtils.TYPE_ARRAY, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                JSONArray jsonArray=(JSONArray)json;
                if(jsonArray.length()==0 && page==1){
                    pullToRefreshListView.setVisibility(GONE);
                    ll_nodata.setVisibility(View.VISIBLE);
                    pullToRefreshListView.onRefreshComplete();
                    return;
                }else{
                    pullToRefreshListView.setVisibility(View.VISIBLE);
                    ll_nodata.setVisibility(View.GONE);
                }

                if(canLoad==false){
                    showToast("没有更多内容啦");
                    pullToRefreshListView.onRefreshComplete();
                    return;
                }
                if(arrayList.size()<pageSize){
                    canLoad=false;
                }
                for (int a=0;a<jsonArray.length();a++){
                    try {
                        arrayList.add(JSON.parseObject(jsonArray.getString(a).replace("null","\"\""),BeanOrder.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                adapterMyOrder.notifyDataSetChanged();
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void fail(String failMsg) {
                pullToRefreshListView.onRefreshComplete();
            }
        });

    }
}
